cd "~/Dropbox/research/Denmark Housing Populism/replicationfiles/"
*REPLICATION FILES
*Article: Sheltering Populists?House Prices and the Support for Populist Parties
*Authors: Ben Ansell, Frederik Hjorth, Jacob Nyrup & Martin Vinæs Larsen
*Software: STATA 16.0
*Dependencies: combomarginsplot, blindschemes

*Replicated in this file: Figures 2, 3 4, G1, H1, I1, J1; Tables 1, A1, D1,.

*dataset used
use "data_dk_ziplvl.dta", clear

*logfile
log using "log_dk_ziplvl.txt", replace t

****************************************************************

**Data preperation
*setting up for panel analysis
xtset district elyear

*defining "treatment" for classic DID analysis
gen longtrend=(hplevel_f-l9.hplevel_f)/l9.hplevel_f if elyear==10 // calculating changes in fixed prices from 1998 to 2015
summarize longt, d
gen  cat=. //creating categories based on changes in fixed prices
replace cat=1 if longt <r(p10) //left behinds=10 percent largest drops
replace cat=4 if longt >r(p90) & longt!=.  //boom=10 percent largest increases
bysort district : egen treat=max(cat) //assigning treatment status across years
ta treat
*rescaling housing price variable to be measured in EUR250
replace hplevel_f=(hplevel_f/7.5)*25

****************************************************************


**Figures and Tables for Main Text

*Drawing Figure 2.
scatter dfshare hplevel, by(year ft, note("") legend(off)  col(2)) msym(Oh) mcolor(black*0.7%10)  || ///scatterplot
lfit dfshare hplevel, scheme(plotplain) lcolor(black) lpattern(solid) /// linear fit
|| lowess dfshare hplevel, lpattern(dash) ytitle("Support for the Danish People's Party (%)") lcolor(black) xtitle("Price Per Square Meter (DKK)") ///
ysize(10) xlabel(0 "0" 1 "10" 2 "20" 3 "30" 4 "40" 5 "50,000") ylabel(0(10)50)  
graph export "figure2.pdf", replace 

*Analysis underlying Figure 3. 
tempfile a b c //creating temporary files
xtreg dfshare i.treat##i.year, vce(cluster zip) //did model
margins, at(year=(1998 1999 2001 2004 2005 2007 2009 2011 2014 2015) treat=(1 4) ) saving(a, replace) //extracting levels of support across treatments
margins, dydx(4.treat) at(year=(1998 1999 2001 2004 2005 2007 2009 2011 2014 2015)) saving(c,replace) //extracting difference in support across treatment and control
reg hplevel_f i.treat##i.year, vce(cluster zip) //Differences in prices
margins, dydx(treat) at(year=(1998 1999 2001 2004 2005 2007 2009 2011 2014 2015)) saving(b, replace) //difference in price btwn treatment and control

*Constructing Figure 3
preserve
use a, clear //include estimates from models a-c
append using b
append using c
replace _at1=10 if _n >30
twoway connect _margin _at2 if _at1==1, lcolor(black*0.2) mcolor(black) xlabel(1998(4)2015, nogrid) xtitle(" ") || /// for left-behind areas
connect _margin _at2 if _at1==4, lcolor(black*0.2) mcolor(black) scheme(plotplain) || /// for boom areas
line _margin _at2 if _at1==.o, lcolor(black*0.8)   lpattern(dash)  || ///  prices
line _margin _at2 if _at1==10, lpattern(solid) ylab(-15(5)35) ytitle("DPP Support (solid), EUR500 (dash)") legend(region(color(none)) label(2 " Boom areas") bmargin(large) ring(0) pos(11) /// difference btwn boom and left-behind
label(1 "Left-behind areas ") order(1 2 4 3) label(4 "Difference btwn boom and left-behind areas") label(3 "Price differential btwn boom and left-behind areas")) || ///
rarea _ci_lb _ci_ub _at2 if _at1==10, yline(0) lcolor(%0) fcolor(black%10) || /// ci's for difference
rarea _ci_lb _ci_ub _at2 if _at1==1,  lcolor(%0) fcolor(black%10)  || /// ci for left-behind
rarea _ci_lb _ci_ub _at2 if _at1==4,  lcolor(%0) fcolor(black%10) || /// ci for boom
rarea _ci_lb _ci_ub _at2 if _at1==.o,  lcolor(%0) fcolor(black%10) // ci for prices
graph export figure3.pdf, replace //exporting graph
restore
*dropping datasets with estimates used to construct figure 3
erase a.dta 
erase b.dta
erase c.dta


*Estimating models for Table 1.

eststo a: xtreg dfdif hpdif i.year, re vce(cluster zip)
eststo b: xtreg dfdif hpdif difpopdens difsolo diftenppl i.year, vce(cluster zip)
eststo c: xtreg dfdif hpdif difpopdens difsolo diftenppl difnwimm i.year, re vce(cluster zip)
eststo d: xtreg dfdif hpdif difpopdens difunem difmedian difsolo diftenppl difnwimm i.year, re vce(cluster zip)
eststo e: xtreg dfdif hpdif difhigh diflow diflow_service diflow_manuf difhighlvl difpopdens difunem difmedian difsolo diftenppl difnwimm i.year , re vce(cluster zip)
foreach x in a b c d e{
estimates restore `x'
estadd local yearfe "$\checkmark$"
}

*calculating effect of going from 10th to 90th percentile
su hplevel, d
di (r(p90)-r(p10))*_b[hpdif]


*Exporting table 1
esttab a b c d e using table1.tex, keep(hpdif difhigh diflow diflow_service diflow_manuf difhighlvl difunemprate difmedianinc difnwimm difpopdens difsolo diftenppl) replace ///
nostar se nomtitles b(%9.1f) nogap ///
label stats(yearfe N rmse, fmt(%8.0f %8.0f %8.3f)   label("Year FE" "Observations" "RMSE")) nonotes addnotes("Clustered standard errors in parentheses.") ///
title(Support for the Danish People's party and Housing Prices} \footnotesize \label{dkfd)


*Estimating models for Figure 4 (test of parrallel trends).
tempfile b1 b2 b3 b4 b5 b6
xtreg l5.dfdif hpdif i.year diflow difhigh  diflow_service diflow_manuf difhighlvl difunemprate difmedianinc difnwimm difpopdens difsolo diftenppl , re vce(cluster zip) //continually add fewer lags
margins, dydx(hpdif) saving(b1, replace)
xtreg l4.dfdif hpdif i.year diflow difhigh  diflow_service diflow_manuf difhighlvl difunemprate difmedianinc difnwimm difpopdens difsolo diftenppl if e(sample)==1 , re vce(cluster zip)
margins, dydx(hpdif) saving(b2, replace)
xtreg l3.dfdif hpdif i.year diflow difhigh  diflow_service diflow_manuf difhighlvl difunemprate difmedianinc difnwimm difpopdens difsolo diftenppl if e(sample)==1, re vce(cluster zip)
margins, dydx(hpdif) saving(b3, replace)
xtreg l2.dfdif hpdif i.year diflow difhigh  diflow_service diflow_manuf difhighlvl difunemprate difmedianinc difnwimm difpopdens difsolo diftenppl if e(sample)==1, re vce(cluster zip)
margins, dydx(hpdif) saving(b4, replace)
xtreg l1.dfdif hpdif i.year diflow difhigh  diflow_service diflow_manuf difhighlvl difunemprate difmedianinc difnwimm difpopdens difsolo diftenppl if e(sample)==1, re vce(cluster zip)
margins, dydx(hpdif) saving(b5, replace)
xtreg dfdif hpdif i.year diflow difhigh diflow_service diflow_manuf difhighlvl difunemprate difmedianinc difnwimm difpopdens difsolo diftenppl if e(sample)==1 , re vce(cluster zip)
margins, dydx(hpdif) saving(b6, replace)


*Contructing figure
preserve
use b1, clear
foreach x in 2 3 4 5 6 {
append using b`x'  //compiling estimates from model
}
gen id=_n
twoway rline _ci_lb _ci_ub id, lpattern(dash)  || /// ci's for estimates
line _margin id, lpattern(solid) lcolor(black) lwidth(medthick) scheme(plotplain) legend(off) /// estimates across different lags
xlabel(1 "t-5" 2  "t-4" 3 "t-3" 4 "t-2" 5 "t-1" 6 "t" , nogrid) ///
xtitle(" ") ytitle("Effect of Changes in Housing Prices", size(medium)) ylabel(-6(1)2) ///
yline(0, lwidth(thick) lcolor(black%50))
graph export "figure4.pdf", replace 
restore
foreach x in 1 2 3 4 5 6 {
erase b`x'.dta
}

****************************************************************

**Figures and Tables for the Appendix

*Difference across election types (appendix A)

recode elyear 2=1 4=2 7=3 9=4 else=., gen(elyear0) //EP elections counter
recode elyear 1=1 3=2 5=3 6=4 8=5 10=6 else=., gen(elyear1) //Parliament elections counter
foreach x in 0 1 {
preserve
 drop dif* dfdif hpdif //dropping differenced variables
 keep if ft==`x' //dropping EP elections and then Parliament elections.
 tsset district elyear`x' //setting up for time series analysis
foreach t in sdshare vshare low high low_service low_manuf highlvl unemprate medianinc nwimm popdens solo tenppl {
gen dif`t'=`t'-l.`t'
} //creating new control variables differenced across one election type
gen dfdif=df-l.df //new DV differenced across one election type
gen hpdif=hplevel-l.hplevel //new independent variable differenced across one election type
eststo a`x': xtreg dfdif c.hpdif diflow difhigh diflow_service diflow_manuf difhighlvl difunemprate difmedianinc difnwimm difpopdens difsolo diftenppl i.year, re vce(cluster zip) //estimating model
restore
}
foreach x in a0 a1{
estimates restore `x'
estadd local yearfe "$\checkmark$"
}
*drawing table 
esttab a0 a1 using tablea1.tex, keep(hpdif difhigh diflow diflow_service diflow_manuf difhighlvl difunemprate difmedianinc difnwimm difpopdens difsolo diftenppl) replace ///
nostar se mtitles("EU" "National") b(%9.1f) nogap ///
label stats(yearfe N rmse, fmt(%8.0f %8.0f %8.3f)   label("Year FE" "Observations" "RMSE")) nonotes addnotes("Clustered standard errors in parentheses.")  ///
title(Support for the Danish People's Party and Housing Prices by Election Type} \footnotesize \label{dkeunat)


**Descriptive statistics (Appendix C)
*fixing some last variable names
la var sdshare "SD support" 
la var vshare "Venstre support"
la var enshare "Enhedslisten support"
la var sfshare "SF support"
la var hplevel_fixed "Fixed Housing Prices (DKK 10,000)"
la var newcomer "Mobility"

file open anyname using "tabled1.txt", write text replace //using file write to construct table
file write anyname  _newline  _col(0)  "\begin{table} [htbp] \centering \caption{Descriptive Statistics, Denmark (precinct) \label{desdnes}} \footnotesize \begin{tabular}{l*{6}{c}}\hline\hline "
file write anyname _newline _col(0) "&Mean & SD & Min & Median & Max & n\\ \hline"
foreach x of var   dfshare sfshare sdshare vshare enshare hplevel hplevel_fixed low high low_service low_manuf highlvl unemprate medianinc nwimm popdens solo tenppl newcomer  {
su  `x' , d
file write anyname  _newline  _col(0) (`"`: var label `x''"') "&" _col(25) %9.2f  (r(mean)) " &" _col(45) %9.2f  (r(sd)) " &" _col(65) %9.2f  (r(min)) " &"   _col(85) %9.2f  (r(p50)) " &" _col(105) %9.2f  (r(max)) " &" _col(125) %9.0f  (r(N)) " \\"
}
file write anyname _newline _col(0) "\hline\hline"
file write anyname _newline _col(0) "\end{tabular}"
file write anyname _newline _col(0) "\end{table}"
file close anyname


*Looking at changes in effect over time (Appendix G)
forvalues x=3/10 { //a rolling analysis that includes three electios per data point
local i=`x'-3
tempfile a`x'
qui xtreg dfdif hpdif difhigh diflow diflow_service diflow_manuf difhighlvl difpopdens difunem difmedian difsolo diftenppl difnwimm i.year if elyear > `i' & elyear <= `x', re vce(cluster zip) //estimating model
qui margins, dydx(hpdif) saving(`a`x'', replace) //saving relevant estimate (i.e., effect of housing prices)
 }
*combining estimates and constructing graph
combomarginsplot  `a3' `a4' `a5' `a6' `a7' `a8' `a9' `a10', scheme(plotplain) yline(0, lpattern(solid)) /// 
 xlabel(1 "98-01" 2 "99-04" 3 "01-05" 4 "04-07" 5 "05-09" 6 "07-11" 7 "09-14" 8 "11-15", nogrid) title(" ") ///
 xtitle(" ") ytitle("Effect of Changes in Housing Prices", size(medium)) recast(scatter) ///
  xline(4.7) text(3 5.5 "Financial Crisis") recastci(rspike) plotopts(msym(O) mcolor(black)) ///
 xtitle(" " "Years Included in the Regression") ylab(-10(2)4)
graph export "figureg1.pdf", replace



*Doing analyses within the capital region (APPENDIX H)
preserve
keep if zip<3000 //dropping zip codes above 3000 (below 3000 is capital region).
eststo a1: xtreg dfdif hpdif i.year , re vce(cluster zip) //estimating models
eststo b1: xtreg dfdif hpdif difpopdens difsolo diftenppl i.year, re vce(cluster zip)
eststo c1: xtreg dfdif hpdif difnwimm difpopdens difsolo diftenppl i.year  if zip <3000, re vce(cluster zip)
eststo d1: xtreg dfdif hpdif difunemprate difmedianinc difnwimm difpopdens difsolo diftenppl i.year  if zip <3000, re vce(cluster zip)
eststo e1: xtreg dfdif hpdif diflow difhigh diflow_service diflow_manuf difhighlvl difunemprate difmedianinc difnwimm difpopdens difsolo diftenppl i.year  if zip <3000, re vce(cluster zip)
foreach x in a1 b1 c1 d1 e1{
estimates restore `x'
estadd local yearfe "$\checkmark$"
}

esttab a1 b1 c1 d1 e1 using tableh1.tex, keep(hpdif difhigh diflow diflow_service diflow_manuf difhighlvl difunemprate difmedianinc difnwimm difpopdens difsolo diftenppl) replace ///writing table 
nostar se nomtitles b(%9.1f) nogap  ///
 label stats(yearfe N rmse, fmt(%8.0f %8.0f %8.3f) label("Year FE" "Observations" "RMSE")) nonotes addnotes("Clustered standard errors in parentheses.")  ///
title(Support for the Danish People's Party and Housing Prices in the Capital Region} \footnotesize \label{dkcapital)
restore


*Looking at effects across levels of mobility (Appendix I)
egen Xbin = cut(newcomer), group(10) //creating 10 groups based on deciles. 
replace Xbin=Xbin+1 

xtreg dfdif c.hpdif##Xbin   diflow difhigh diflow_service diflow_manuf difhighlvl difpopdens difunem difmedian difsolo diftenppl difnwimm i.year, re vce(cluster zip) //looking at interaction iffects
margins, dydx(hpdif) over(Xbin) //marginal effects across mobility deciles
*creating figure plotting effects
marginsplot , scheme(plotplain) recastci(rline) ///
 ylab(-6(1)0, nogrid) xlab(1/10) yline(0) ///
 ytitle("Effect on Support for DPP") xtitle("Effect at Deciles of Mobility") title(" ") 
graph export "figurei1.pdf", replace


*A More restrictive model where we add unit FE to our FD model (Appendix J)
eststo a1: xtreg dfdif hpdif diflow difhigh  i.year , fe vce(cluster zip) //estimating models
eststo b1: xtreg dfdif hpdif diflow difhigh  difpopdens difsolo diftenppl i.year , fe vce(cluster zip)
eststo c1: xtreg dfdif hpdif diflow difhigh  difnwimm difpopdens difsolo diftenppl i.year, fe vce(cluster zip)
eststo d1: xtreg dfdif hpdif diflow difhigh  difunemprate difmedianinc difnwimm difpopdens difsolo diftenppl i.year , fe vce(cluster zip)
eststo e1: xtreg dfdif hpdif diflow difhigh  diflow_service diflow_manuf difhighlvl difunemprate difmedianinc difnwimm difpopdens difsolo diftenppl i.year, fe vce(cluster zip)
foreach x in a1 b1 c1 d1 e1{
estimates restore `x'
estadd local yearfe "$\checkmark$"
}
esttab a1 b1 c1 d1 e1 using tablej1.tex, keep( hpdif  difhigh diflow diflow_service diflow_manuf difhighlvl difunemprate difmedianinc difnwimm difpopdens difsolo diftenppl) replace /// writing table
nostar se nomtitles b(%9.1f) nogap label stats(yearfe yearfe N rmse, fmt(%8.0f %8.0f %8.0f %8.3f)  label("Precinct Trend FE" "Year FE" "Observations" "RMSE")) nonotes addnotes("Clustered standard errors in parentheses.") ///
title(Suppport for the Danish People's party using First Differences and Fixed Effects } \footnotesize \label{dkfefd)



*Housing prices and support for other parties (Appendix K)
*creating new variables
gen difleft=sdshare+sfshare+enshare-(l.sdshare+l.sfshare+l.enshare) //left
gen difextleft=sfshare+enshare-(l.sfshare+l.enshare) //socialist left
gen difsd=sdshare-(l.sdshare) //social democratic party
gen difoppo=difsd-dfdif if year>2011 | year <2002 //difference ins support for non-incumbents
replace difoppo=difvshare-dfdif if year<2012 | year >2001 //difference ins support for non-incumbents
*estimating models with full set of controls and different dependent variables
eststo a: xtreg difleft hpdif diflow difhigh diflow_service diflow_manuf difhighlvl difpopdens difunem difmedian difsolo diftenppl difnwimm i.year,  vce(cluster zip)
eststo b: xtreg difextleft hpdif diflow difhigh  diflow_service diflow_manuf difhighlvl difpopdens difunem difmedian difsolo diftenppl difnwimm i.year ,  vce(cluster zip)
eststo c: xtreg difsd hpdif diflow difhigh  diflow_service diflow_manuf difhighlvl difpopdens difunem difmedian difsolo diftenppl difnwimm i.year , vce(cluster zip)
eststo d: xtreg difvshare hpdif diflow difhigh diflow_service diflow_manuf difhighlvl difpopdens difunem difmedian difsolo diftenppl difnwimm i.year  ,  vce(cluster zip)
eststo e: xtreg difoppo hpdif diflow difhigh diflow_service diflow_manuf difhighlvl difpopdens difunem difmedian difsolo diftenppl difnwimm i.year ,  vce(cluster zip)
foreach x in a b c d e{
estimates restore `x'
estadd local yearfe "$\checkmark$"
}
esttab a b c d e using tablek1.tex, keep(hpdif difhigh diflow diflow_service diflow_manuf difhighlvl ///
difunemprate difmedianinc difnwimm difpopdens difsolo diftenppl) replace /// writing table
nostar nonum se b(%9.1f) nogap  label stats(yearfe N rmse, fmt(%8.0f %8.0f %8.3f)   label("Year FE" "Observations" "RMSE")) nonotes addnotes("Clustered standard errors in parentheses.")  ///
title(Support for Selected Other Parties and Housing Prices} \footnotesize \label{dksocial) ///
mtitles("Left" "Socialist" "Social Democratic" "Liberal Party" "Non-incumbent")


*closing log
log close

